package com.question.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.question.Constant.BaseConstant;
import com.question.annotation.GlobalInterceptor;
import com.question.annotation.VerifyParams;
import com.question.common.PageResult;
import com.question.common.Result;
import com.question.entity.Paper;
import com.question.entity.Records;
import com.question.mapper.RecordsMapper;
import com.question.service.PaperService;
import com.question.service.RecordsService;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/paper")
public class PaperController {
    @Resource
    private PaperService paperService;
    @Resource
    private RecordsMapper recordsMapper;
    @Resource
    private RecordsService recordsService;
    @Resource
    private RedisTemplate redisTemplate;

    @RequestMapping("/add")
    @GlobalInterceptor(checkLogin = true, checkParam = true)
    public Result add(@VerifyParams String name, @VerifyParams String description, @VerifyParams String cover) {

        paperService.add(name, description, cover);
        return Result.success("成功");
    }

    @RequestMapping("/list")
    @GlobalInterceptor(checkLogin = true, checkParam = true)
    public Result list(Integer pageNo, Integer pageSize, String keyword) {
        PageResult pageResult = paperService.getByPage(pageNo, pageSize, keyword);
        return Result.success(pageResult);
    }

    @RequestMapping("/edit")
    @GlobalInterceptor(checkLogin = true, checkParam = true)
    public Result edit(Paper paper) {
        paperService.edit(paper);
        return Result.success();
    }

    @RequestMapping("/delete")
    @GlobalInterceptor(checkLogin = true, checkParam = true)
    public Result delete(Integer id) {
        paperService.delete(id);
        return Result.success();
    }

    @RequestMapping("/publish")
    @GlobalInterceptor(checkLogin = true, checkParam = true)
    public Result publish(Integer id) {
        paperService.publish(id);
        return Result.success();
    }

    @RequestMapping("/all")
    @GlobalInterceptor(checkLogin = true, checkParam = true)
    public Result all() {
        List<Paper> list = paperService.list();
        return Result.success(list);
    }

    @RequestMapping("/publishlist")
    @GlobalInterceptor(checkLogin = true, checkParam = true)
    public Result publishlist() {
        LambdaQueryWrapper<Paper> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Paper::getStatus, 1);
        List<Paper> list = paperService.list(lambdaQueryWrapper);
        return Result.success(list);
    }

    @RequestMapping("/detail")
    @GlobalInterceptor(checkLogin = true, checkParam = true)
    public Result detail(Integer id) {
        Paper paper = paperService.getdetail(id);
        return Result.success(paper);

    }

    @RequestMapping("/answerdetail")
    @GlobalInterceptor(checkLogin = true, checkParam = true)
    public Result answerdetail(Integer id, Integer uid) {
        List<Records> list = recordsService.answerdetail(id, uid);
        return Result.success(list);

    }

    @RequestMapping("/use")
    @GlobalInterceptor(checkLogin = true, checkParam = true)
    public Result use() {
        List<String> list = new ArrayList<>();
        List<Integer> list1 = new ArrayList<>();
        List<Paper> papers = paperService.list();
        for (Paper paper : papers) {
            list.add(paper.getName());
            list1.add(paper.getUsecount());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("x", list);
        map.put("y", list1);
        return Result.success(map);
    }

    @RequestMapping("/popular")
    @GlobalInterceptor(checkLogin = true, checkParam = true)
    public Result popular() {
        List<String> list = new ArrayList<>();

        ZSetOperations<String, Object> zSetOperations = redisTemplate.opsForZSet();
        Set<Object> popularIds = redisTemplate.opsForZSet().reverseRange(BaseConstant.PAPER, 0, 6);
        List<Long> list1 = popularIds.stream().map(id -> Long.valueOf(id.toString())).collect(Collectors.toList());
        List<Double> list2 = new ArrayList<>();
        for (Long id : list1) {

            list.add(paperService.getdetail(Math.toIntExact(id)).getName());
            list2.add(zSetOperations.score((String) BaseConstant.PAPER, id));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("x", list);
        map.put("y", list2);
        return Result.success(map);


    }


}
